package com.bianmin.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bianmin.Model.Article;
import com.bianmin.Model.Comment;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    //查询所有满足置顶的数据
    @Select("select a.*, b.nick_name as nickName, b.icon as icon from article a left join  user b on a.user_id = b.id where ifnull(a.top_end_time,0) > unix_timestamp()")
    List<Article> topArticleList();

    //文章列表
    @Select("SELECT a.*, b.nick_name AS nickName, b.icon AS icon\n" +
            "FROM article a LEFT JOIN user b ON a.user_id = b.id\n" +
            "WHERE IFNULL(a.top_end_time,0) < UNIX_TIMESTAMP()\n" +
            "ORDER BY id DESC\n" +
            "LIMIT #{page}, #{pageSize};")
    List<Article>articleList(int page, int pageSize);

    //搜索文章
    @Select("select * from article where title like #{keyWord}")
    List<Article> searchArticle(String keyWord);

    //删除文章
    @Delete("DELETE FROM article WHERE id = #{id}")
    boolean deleteArticle(String id);

    //收藏文章
    @Insert("INSERT INTO collection SET user_id = #{userId},article_id = #{articleId}")
    boolean collectionArticle(String userId, String articleId);

    //取消收藏
    @Delete("DELETE FROM collection WHERE id = #{id}")
    boolean unCollectionArticle(String id);

    //添加评论
    @Insert("INSERT INTO comment SET article_id = #{articleId}, content = #{content},user_id = #{userId},create_time = #{time}")
    boolean commentArticle(String articleId,String content, String userId, String time);

    //根据文章id查询发帖人的id
    @Select("select user_id from article where id = #{articleId}")
    String queryUserIdWithArticleId(String articleId);

    //单独删除评论
    @Delete("delete from comment where id = #{commentId}")
    boolean deleteComment(String commentId);

    //批量删除评论，当删除文章时
    @Delete("DELETE FROM comment WHERE article_id = #{articleId}")
    void deleteComments(String articleId);

    //文章详情
    @Select("SELECT a.*, b.nick_name AS nickName, b.icon AS icon " +
            "FROM article a JOIN user b ON a.user_id = b.id " +
            "WHERE a.id = #{articleId};")
    Article articleDetail(String articleId);

    //文章相关的评论
    @Select("SELECT t1.* , t2.nick_name AS nickName, t2.icon AS icon FROM comment t1\n" +
            "JOIN user t2 ON t1.user_id = t2.id\n" +
            "WHERE t1.article_id = #{articleId} order by id desc")
    List<Comment> articleComments(String articleId);

    //查询文章呗收藏的状态
    @Select("select * from collection where article_id = #{articleId} and user_id = #{userId}")
    String queryArticleCollectionStatus(String articleId, String userId);

    //查询文章呗收藏的数量
    @Select("select count(*) from collection where article_id = #{articleId}")
    Integer countForCollectionWithArticle(String articleId);


    //根据文章id，查询用户openid
    @Select("select t2.open_id as openId, t1.title as title , t3.nick_name as name from article t1 join user t2 on t1.user_id = t2.id join user t3 on #{commentUserId} = t3.id where t1.id = #{articleId}")
    Map<String,String> queryOpenIdWithArticleId(String articleId, String commentUserId);

}
